<template>
    <div class="apply-manage">
      <el-card class="apply-card">
        <template #header>
          <div class="card-header">
            <span class="header-title">员工请假申请审批</span>
          </div>
        </template>
  
        <!-- 搜索表单 -->
        <div class="search-area">
          <el-form :inline="true" :model="searchForm">
            <el-form-item label="员工编号">
              <el-input
                v-model="searchForm.employeeNo"
                placeholder="请输入员工编号"
                clearable
                @keyup.enter="handleSearch"
                class="search-input"
              />
            </el-form-item>
            <el-form-item label="员工姓名">
              <el-input
                v-model="searchForm.name"
                placeholder="请输入员工姓名"
                clearable
                @keyup.enter="handleSearch"
                class="search-input"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSearch" class="search-btn">
                <el-icon><Search /></el-icon>查询
              </el-button>
              <el-button @click="resetSearch" class="reset-btn">
                <el-icon><RefreshRight /></el-icon>重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>
  
        <el-table
          v-loading="loading"
          :data="tableData"
          style="width: 100%"
          border
          class="apply-table"
          :header-cell-style="{
            background: '#f8faff',
            color: '#303133',
            fontWeight: '600',
            fontSize: '14px'
          }"
        >
          <el-table-column prop="employeeNo" label="员工编号" width="120" />
          <el-table-column prop="employeeName" label="员工姓名" width="120" />
          <el-table-column label="请假类型" width="120">
            <template #default="{ row }">
              <el-tag :type="leaveTypeMap[row.leaveType]?.type" effect="light">
                {{ row.leaveTypeDesc || leaveTypeMap[row.leaveType]?.label || '未知类型' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="startDate" label="开始时间" width="180" />
          <el-table-column prop="endDate" label="结束时间" width="180" />
          <el-table-column prop="reason" label="请假原因" min-width="200" show-overflow-tooltip />
          <el-table-column label="状态" width="100">
            <template #default="{ row }">
              <el-tag :type="row.status === 0 ? 'warning' : row.status === 1 ? 'success' : 'danger'" effect="light">
                {{ row.statusDesc }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="approverNo" label="审批人工号" width="120" />
          <el-table-column prop="approvalTime" label="审批时间" width="180" />
          <el-table-column prop="approvalRemark" label="审批备注" min-width="150" show-overflow-tooltip />
          <el-table-column label="操作" width="200" fixed="right" align="center">
            <template #default="{ row }">
              <div class="button-container">
                <el-button
                  v-if="row.status === 0"
                  type="success"
                  size="small"
                  @click="handleApprove(row)"
                  class="action-btn approve-btn"
                >
                  <el-icon><Check /></el-icon>通过
                </el-button>
                <el-button
                  v-if="row.status === 0"
                  type="danger"
                  size="small"
                  @click="handleReject(row)"
                  class="action-btn reject-btn"
                >
                  <el-icon><Close /></el-icon>拒绝
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
  
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 30, 50]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            background
            class="custom-pagination"
          />
        </div>
      </el-card>

      <!-- 审批弹窗 -->
      <el-dialog
        v-model="dialogVisible"
        :title="dialogType === 'approve' ? '审批通过' : '审批拒绝'"
        width="500px"
        class="approval-dialog"
      >
        <el-form :model="approvalForm" ref="approvalFormRef" :rules="approvalRules">
          <el-form-item 
            label="审批理由" 
            prop="approvalRemark"
            :label-width="'100px'"
          >
            <el-input
              v-model="approvalForm.approvalRemark"
              type="textarea"
              :rows="4"
              placeholder="请输入审批理由"
              class="form-textarea"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">
              <el-icon><Close /></el-icon>取消
            </el-button>
            <el-button type="primary" @click="submitApproval" :loading="submitLoading" class="submit-btn">
              <el-icon><Check /></el-icon>确认
            </el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </template>
<script setup>
import { ref, onMounted, reactive } from 'vue'
import { getApplyList, approveApply, rejectApply } from '@/api/admin/apply'
import { ElMessage } from 'element-plus'
import { 
  Search, 
  RefreshRight, 
  Check, 
  Close
} from '@element-plus/icons-vue'

// 请假类型映射
const leaveTypeMap = {
  1: { label: '事假', type: '' },
  2: { label: '病假', type: 'warning' },
  3: { label: '年假', type: 'success' },
  4: { label: '婚假', type: 'danger' },
  5: { label: '产假', type: 'danger' },
  6: { label: '丧假', type: 'info' },
  7: { label: '其他', type: 'info' }
}

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 搜索条件
const searchForm = reactive({
  employeeNo: '',
  name: '',
  status: ''
})

// 获取当前管理员编号
const adminNo = localStorage.getItem('employeeNo')

// 弹窗相关
const dialogVisible = ref(false)
const dialogType = ref('approve') // 'approve' 或 'reject'
const submitLoading = ref(false)
const approvalFormRef = ref(null)

// 审批表单
const approvalForm = reactive({
  id: '',
  approverNo: '',
  status: 1,
  approvalRemark: ''
})

// 表单验证规则
const approvalRules = {
  approvalRemark: [
    { required: true, message: '请输入审批理由', trigger: 'blur' },
    { min: 2, max: 200, message: '长度在 2 到 200 个字符', trigger: 'blur' }
  ]
}

// 获取请假申请列表
const fetchApplyList = async () => {
  if (!adminNo) {
    ElMessage.error('获取管理员信息失败')
    return
  }

  loading.value = true
  try {
    const params = {
      adminNo,
      employeeNo: searchForm.employeeNo || '',
      name: searchForm.name || '',
      status: searchForm.status,
      current: currentPage.value,
      size: pageSize.value
    }
    const res = await getApplyList(params)
    tableData.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    console.error('获取请假申请列表失败:', error)
    ElMessage.error('获取请假申请列表失败')
  } finally {
    loading.value = false
  }
}

// 处理搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchApplyList()
}

// 重置搜索
const resetSearch = () => {
  searchForm.employeeNo = ''
  searchForm.name = ''
  searchForm.status = ''
  currentPage.value = 1
  fetchApplyList()
}

// 处理页码变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchApplyList()
}

// 处理每页条数变化
const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
  fetchApplyList()
}

// 打开审批弹窗
const openApprovalDialog = (row, type) => {
  dialogType.value = type
  approvalForm.id = row.id
  approvalForm.approverNo = adminNo
  approvalForm.status = type === 'approve' ? 1 : 2
  approvalForm.approvalRemark = ''
  dialogVisible.value = true
}

// 提交审批
const submitApproval = async () => {
  if (!approvalFormRef.value) return
  
  await approvalFormRef.value.validate(async (valid) => {
    if (valid) {
      submitLoading.value = true
      try {
        const apiMethod = dialogType.value === 'approve' ? approveApply : rejectApply
        await apiMethod(approvalForm)
        
        ElMessage.success(dialogType.value === 'approve' ? '审批通过成功' : '已拒绝该申请')
        dialogVisible.value = false
        fetchApplyList()
      } catch (error) {
        console.error('审批失败:', error)
        ElMessage.error('审批失败')
      } finally {
        submitLoading.value = false
      }
    }
  })
}

// 修改原有的审批处理函数
const handleApprove = (row) => {
  openApprovalDialog(row, 'approve')
}

const handleReject = (row) => {
  openApprovalDialog(row, 'reject')
}

onMounted(() => {
  if (!adminNo) {
    ElMessage.error('获取管理员信息失败')
    return
  }
  fetchApplyList()
})
</script>

<style scoped lang="scss">
.apply-manage {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  
  .apply-card {
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
    overflow: hidden;
    
    &:hover {
      box-shadow: 0 12px 32px rgba(24, 144, 255, 0.1);
      transform: translateY(-2px);
    }
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #f0f0f0;
    
    .header-title {
      font-size: 18px;
      font-weight: 600;
      color: #303133;
      position: relative;
      padding-left: 12px;
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 4px;
        height: 18px;
        background: #1890ff;
        border-radius: 2px;
      }
    }
  }

  .search-area {
    margin-bottom: 20px;
    padding: 20px;
    background-color: #f8faff;
    border-radius: 8px;
    
    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #606266;
    }
    
    .search-input {
      width: 220px;
      
      :deep(.el-input__wrapper) {
        border-radius: 8px;
        box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1) inset;
        
        &:hover, &.is-focus {
          box-shadow: 0 0 0 1px #1890ff inset;
        }
      }
    }
    
    .search-btn {
      background: linear-gradient(135deg, #1890ff, #36a6ff);
      border: none;
      border-radius: 20px;
      padding: 8px 16px;
      transition: all 0.3s ease;
      box-shadow: 0 2px 6px rgba(24, 144, 255, 0.2);
      margin-right: 10px;
      
      .el-icon {
        margin-right: 4px;
      }
      
      &:hover {
        background: linear-gradient(135deg, #40a9ff, #5cbbff);
        box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
        transform: translateY(-2px);
      }
      
      &:active {
        background: linear-gradient(135deg, #096dd9, #1890ff);
        transform: translateY(0);
      }
    }
    
    .reset-btn {
      background: #f5f7fa;
      color: #606266;
      border: none;
      border-radius: 20px;
      padding: 8px 16px;
      transition: all 0.3s ease;
      
      .el-icon {
        margin-right: 4px;
      }
      
      &:hover {
        background: #e6f7ff;
        color: #1890ff;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        transform: translateY(-2px);
      }
      
      &:active {
        background: #dcf0fd;
        transform: translateY(0);
      }
    }
  }
  
  .apply-table {
    margin: 16px 0;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
    
    :deep(.el-table__row) {
      transition: all 0.3s;
      
      &:hover {
        background-color: #f8faff !important;
      }
    }
    
    :deep(.el-table__cell) {
      padding: 10px 0;
    }
    
    :deep(.el-tag) {
      padding: 0 10px;
      height: 28px;
      line-height: 28px;
      border-radius: 4px;
      font-weight: 500;
      font-size: 12px;
      
      &.el-tag--success {
        background-color: rgba(82, 196, 26, 0.08);
        border-color: rgba(82, 196, 26, 0.2);
        color: #52c41a;
      }
      
      &.el-tag--info {
        background-color: rgba(144, 147, 153, 0.08);
        border-color: rgba(144, 147, 153, 0.2);
        color: #909399;
      }
      
      &.el-tag--warning {
        background-color: rgba(250, 173, 20, 0.08);
        border-color: rgba(250, 173, 20, 0.2);
        color: #faad14;
      }
      
      &.el-tag--danger {
        background-color: rgba(245, 34, 45, 0.08);
        border-color: rgba(245, 34, 45, 0.2);
        color: #f5222d;
      }
      
      &.el-tag--primary {
        background-color: rgba(24, 144, 255, 0.08);
        border-color: rgba(24, 144, 255, 0.2);
        color: #1890ff;
      }
    }
    
    .action-btn {
      font-size: 13px;
      font-weight: 500;
      transition: all 0.3s;
      margin: 0;
      border-radius: 4px;
      padding: 8px 12px;
      display: flex;
      align-items: center;
      justify-content: center;
      
      &:hover {
        transform: translateY(-2px);
        opacity: 0.9;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      }
      
      &:active {
        transform: translateY(0);
      }
    }
    
    .approve-btn {
      background: linear-gradient(135deg, #52c41a, #72db3f);
      border: none;
      border-radius: 16px;
      padding: 6px 12px;
      transition: all 0.3s;
      color: #fff;
      font-size: 12px;
      font-weight: 500;
      
      .el-icon {
        margin-right: 4px;
      }
      
      &:hover {
        background: linear-gradient(135deg, #72db3f, #95eb64);
        box-shadow: 0 4px 12px rgba(82, 196, 26, 0.2);
        transform: translateY(-2px);
      }
      
      &:active {
        background: linear-gradient(135deg, #389e0d, #52c41a);
        transform: translateY(0);
      }
    }
    
    .reject-btn {
      background: linear-gradient(135deg, #f5222d, #f76560);
      border: none;
      border-radius: 16px;
      padding: 6px 12px;
      transition: all 0.3s;
      color: #fff;
      font-size: 12px;
      font-weight: 500;
      
      .el-icon {
        margin-right: 4px;
      }
      
      &:hover {
        background: linear-gradient(135deg, #f76560, #f9888e);
        box-shadow: 0 4px 12px rgba(245, 34, 45, 0.2);
        transform: translateY(-2px);
      }
      
      &:active {
        background: linear-gradient(135deg, #cf1322, #f5222d);
        transform: translateY(0);
      }
    }
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    padding: 10px 0;
    
    :deep(.custom-pagination) {
      padding: 0;
      
      .el-pagination__total {
        margin-right: 16px;
        color: #606266;
        font-weight: 500;
      }
      
      .el-pagination__sizes {
        margin-right: 16px;
        
        .el-select .el-input {
          width: 110px;
          
          .el-input__wrapper {
            background-color: #f5f7fa;
            border-radius: 20px;
          }
        }
      }
      
      button {
        background-color: #f5f7fa;
        border-radius: 4px;
        margin: 0 3px;
        color: #606266;
        transition: all 0.2s ease;
        
        &:hover {
          color: #1890ff;
          background-color: #e6f7ff;
        }
        
        &:disabled {
          color: #c0c4cc;
        }
      }
      
      .btn-prev, .btn-next {
        padding: 0 8px;
        
        &:hover {
          background-color: #e6f7ff;
        }
      }
      
      .el-pager {
        li {
          background-color: #f5f7fa;
          border-radius: 4px;
          margin: 0 3px;
          color: #606266;
          font-weight: 500;
          min-width: 32px;
          transition: all 0.2s ease;
          
          &:hover {
            color: #1890ff;
            background-color: #e6f7ff;
          }
          
          &.is-active {
            background-color: #1890ff;
            color: #fff;
            font-weight: 600;
            box-shadow: 0 2px 6px rgba(24, 144, 255, 0.2);
            
            &:hover {
              background-color: #40a9ff;
            }
          }
        }
      }
      
      .el-pagination__jump {
        margin-left: 16px;
        color: #606266;
        
        .el-input__wrapper {
          background-color: #f5f7fa;
          border-radius: 4px;
        }
      }
    }
  }

  .dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
  }
  
  :deep(.approval-dialog) {
    border-radius: 12px;
    overflow: hidden;
    
    .el-dialog__header {
      padding: 16px 20px;
      border-bottom: 1px solid #f0f0f0;
      margin: 0;
      
      .el-dialog__title {
        font-size: 18px;
        font-weight: 600;
        color: #303133;
      }
    }
    
    .el-dialog__body {
      padding: 24px;
    }
    
    .el-dialog__footer {
      padding: 16px 20px;
      border-top: 1px solid #f0f0f0;
    }
    
    .el-form-item__label {
      font-weight: 600;
      color: #606266;
    }
    
    .form-textarea {
      .el-textarea__inner {
        border-radius: 8px;
        padding: 10px 12px;
        min-height: 120px !important;
        box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1) inset;
        transition: all 0.3s;
        
        &:hover, &:focus {
          box-shadow: 0 0 0 1px #1890ff inset;
        }
      }
    }
    
    .dialog-footer {
      .el-button {
        border-radius: 8px;
        padding: 8px 20px;
        font-weight: 500;
        transition: all 0.3s;
        
        .el-icon {
          margin-right: 4px;
        }
        
        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
      }
      
      .submit-btn {
        background: linear-gradient(135deg, #1890ff, #36a6ff);
        border-color: #1890ff;
        
        &:hover {
          background: linear-gradient(135deg, #40a9ff, #5cbbff);
          border-color: #40a9ff;
        }
      }
    }
  }

  .button-container {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
  }
}
</style> 